//
//  HNSetWorkVC.m
//  优学荟管理系统
//
//  Created by 星道 on 2017/9/25.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HNSetWorkVC.h"
#import "ZYHomeWorkManagerCourseView.h"
#import "ZYHomeWorkManagerModel.h"

@interface HNSetWorkVC ()
{

    IBOutlet UIButton *_next;
    IBOutlet UITextField *_typeText;
    IBOutlet UITextField *_courseText;
    NSInteger _type ;
    NSInteger _courseIndex;
}
@end

@implementation HNSetWorkVC

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"布置作业";

    [_typeText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_courseText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    [_next setBackgroundImage:[UIImage imageNamed:@"btn_next_default"] forState:(UIControlStateSelected)];
    [_next setBackgroundImage:[LLTools createImageWithColor:ColorRGB(220, 220, 220)] forState:(UIControlStateNormal)];
    [_next setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
    [_next setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];

    [self textFieldDidChange:nil];
}


#pragma mark - 请选择课程类目
- (IBAction)ClickType:(UIButton *)sender {

    // alert
    [self ClickTypeAlert:@"请选择课程类目"];
}

- (void)ClickTypeAlert:(NSString *)title {

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:title preferredStyle:(UIAlertControllerStyleAlert)];
    [alert addAction:[UIAlertAction actionWithTitle:@"兴趣课" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        _type = 2;
        _typeText.text = @"兴趣课";
        _courseIndex = 0;
        _courseText.text = @"";
        [self textFieldDidChange:_typeText];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"文化课" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        _type = 1;
        _typeText.text = @"文化课";
        _courseIndex = 0;
        _courseText.text = @"";
        [self textFieldDidChange:_typeText];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}



#pragma mark - 请选择开课课程
- (IBAction)ClickCourse:(UIButton *)sender {

    if (_type == 0) {

        [HUD showError:@"请先选择课程类目"];
        return;
    }

    // 得到数据后弹框
    [self RefreshDataWithCourse:^(NSArray<ZYHomeWorkManagerModel *> *Arr) {

        dispatch_async(dispatch_get_main_queue(), ^{
            // 课程View
            [ZYHomeWorkManagerCourseView ConfigWithArr:Arr Complete:^(NSInteger Index) {

                ZYHomeWorkManagerModel *Model = Arr[Index];
                _courseText.text = Model.name;
                _courseIndex     = [Model.ID integerValue];
                [self textFieldDidChange:nil];
            }];
        });
    }];
}

// 根据type获取网络数据
- (void)RefreshDataWithCourse:(void (^)(NSArray <ZYHomeWorkManagerModel*>*Arr))Complete {

    [HUD showHUD];
    NSDictionary *Params = @{
                             @"action"  :@"getLessonByCategory",
                             @"type"    :@(_type),
                             @"uid"     :[AccountTool shareAccountTool].currentAccount.uid
                             };
    [NetManager Get:LLBaseURL(@"assignHomework.do") Auth:YES Params:Params Success:^(id Obj) {

        if ([Obj[@"code"] isEqualToNumber:@1000]) {

            NSMutableArray *Arr = [NSMutableArray array];
            [Obj[@"t"] enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

                [Arr addObject:[ZYHomeWorkManagerModel HNObjectValuesForKey:obj]];
            }];

            Complete ? Complete ([Arr copy]):nil;

        } else {

            [HUD showError:Obj[@"msg"]];
        }
    } Fail:^(NSError *Error) {
        
        [HUD showError:NetErr];
    }];
}

#pragma mark - 下一步
- (IBAction)Next:(UIButton *)sender {

    if (_courseIndex <= 0) {
        return;
    }

    

}

- (void)textFieldDidChange:(UITextField *)textField {

    _next.selected =  _typeText.text.length <= 0 ? NO:_courseText.text.length <=0 ? NO:YES;
    _next.userInteractionEnabled = !_next.selected;
}
@end
